<?php

namespace app\admin\controller\benefit;

use think\App;
use app\Request;
use think\response\Json;
use think\facade\Cache;

use app\admin\service\TriggerService;
use app\common\controller\AdminController;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\NodeAnnotation;

use app\admin\model\BenefitConfig;

#[ControllerAnnotation(title: '计算参数配置')]
class Config extends AdminController
{
    
    private array $notes;

    #[NodeAnnotation(ignore: ['add', 'delete', 'edit', 'export', 'modify', 'recycle'])]
    protected array $ignoreNode;

    private array $month_type = [
        ['name' => '上月', 'value' => '0'],
        ['name' => '本月', 'value' => '1'],
    ];

    private array $carry_type = [
        ['name' => '四舍五入', 'value' => '1'],
        ['name' => '直接舍弃', 'value' => '0'],
        ['name' => '直接进位', 'value' => '2'],
    ];

    private array $after_calculate_type = [
        ['name' => '月', 'value' => '1'],
        ['name' => '天', 'value' => '0'],
    ];

    //小数位数
    private array $reserve_digit_array = [
        ['name' => '1', 'value' => '1'],
        ['name' => '2', 'value' => '2'],
        ['name' => '3', 'value' => '3'],
        ['name' => '4', 'value' => '4'],
    ];

    ///措施前产量向前推次数
    private array $measures_before_times_array = [
        ['name' => '1', 'value' => '1'],
        ['name' => '2', 'value' => '2'],
        ['name' => '3', 'value' => '3'],
    ];

    private array $before_calculate_type = [];

    //参数验证
    private array $rule = [
        'carry_type'  => 'require|in:0,1,2',
        'reserve_digit'   => 'require|in:1,2,3,4',
        'measures_before_times'   => 'require|in:1,2,3',
        'measures_before_interval'   => 'require|integer',
        'measures_before_type'   => 'require|in:0,1',
        'measures_before_yl'   => 'require|number',
        'measures_after_interval'   => 'require|integer',
        'measures_after_type'   => 'require|in:0,1',
        'measures_after_hs'   => 'require|number',
        'oil_price'   => 'require|number'
    ];
    private array $message = [
        'carry_type.require'   => "进位方式为必填项",
        'carry_type.in'   => "进请选择正确的进位方式",
        'reserve_digit.require'   => '保留小数位数为必填项',
        'reserve_digit.in'   => '请选择正确的小数位数',
        'measures_before_times.require'   => '措施前产量向前推算次数为必填项',
        'measures_before_times.in'   => '请选择正确的措施前产量向前推算次数',
        'measures_before_interval.require'   => '措施前产量推算间隔为必填项',
        'measures_before_interval.integer'   => '措施前产量推算间隔只能为整数',
        'measures_before_type.require'   => '措施前产量推算按月/日为必填项',
        'measures_before_type.in'   => '请选正确的择措施前产量推算方式',
        'measures_before_yl.require'   => '措施前产量计算条件为必填项',
        'measures_before_yl.number'   => '产量只能为数字',
        'measures_after_interval.require'   => '措施后产量推算间隔为必填项',
        'measures_after_interval.integer'   => '措施后产量推算间隔只能为整数',
        'measures_after_type.require'   => '措施后产量推算按月/日为必填项',
        'measures_after_type.in'   => '请选正确的择措施后产量推算方式',
        'measures_after_hs.require'   => '措施后产量计算条件为必填项',
        'measures_after_hs.number'   => '含水量只能为数字',
        'oil_price.require'   => '油价为必填项',
        'oil_price.number'   => '油价只能为数字',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        self::$model = new \app\admin\model\BenefitConfig();
        $notes = self::$model::$notes;

        $this->notes = $notes;
        $this->assign(compact('notes'));

        $this->before_calculate_type = $this->after_calculate_type;
    }

    #[NodeAnnotation(title: '列表', auth: true)]
    public function index(Request $request): Json|string
    {
        $this->assign('month_type_array', $this->month_type);

        $this->assign('carry_type_array', $this->carry_type);
        $this->assign('carry_type', benefitConfig('carry_type'));

        $this->assign('reserve_digit_array', $this->reserve_digit_array);
        $this->assign('reserve_digit', benefitConfig('reserve_digit'));

        $this->assign('before_calculate_type_array', $this->before_calculate_type);
        $this->assign('before_calculate_type', benefitConfig('measures_before_type'));

        $this->assign('measures_before_times_array', $this->measures_before_times_array);
        $this->assign('measures_before_times', benefitConfig('measures_before_times'));

        $this->assign('after_calculate_type_array', $this->after_calculate_type);
        $this->assign('after_calculate_type', benefitConfig('measures_after_type'));

        return $this->fetch();
    }

    #[NodeAnnotation(title: '保存', auth: true)]
    public function save(Request $request)
    {
        $this->checkPostRequest();
        $post  = $request->post();
        $this->validate($post, $this->rule, $this->message);
        try {
            BenefitConfig::update($post, ['user' => session('admin.id')]);
            TriggerService::updateBenefitConfig();
        } catch (\Exception $e) {
            $this->error(config("code.operation_failed") . $e->getMessage());
        }
        $this->success(config("code.operation_success"));
    }
}
